package dao.mysql;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import model.AppliedDataDTO;
import model.DetailDTO;
import model.UserDTO;
import exception.DAOSystemException;
import dao.UserDAO;
import dao.DetailDAO;

//-- Hnin phyho--
public class DetailDAOImpl implements DetailDAO{
	
	MySqlImpl mysqlImpl = new MySqlImpl();
	
	
	public DetailDTO findAppliedUser(String userId) throws DAOSystemException{
		String selectSql = "Select u.id,u.Name,p.role,l.status,l.applyDate,t.type,l.from,l.to,l.reason,l.leaveNature,l.workDissemination,l.contactDetails"+
							" From user as u"+ 
							" join `leaveapplicationdb`.`position` as p"+
							" on p.id=u.positionId"+
							" join `leaveapplicationdb`.`leave` as l"+ 
							" on u.id=l.userId"+ 
							" join `leaveapplicationdb`.`leavetype` as t"+ 
							" on t.id=l.leaveTypeId"+ 
							" Where u.id='"+userId.trim()+"';";
		DetailDTO oneUser = null;
		try{
			ResultSet rs = mysqlImpl.get(selectSql); 
			
			if (rs.next()){
			
				oneUser = new DetailDTO();
				oneUser.setUserId(rs.getString("id"));
				oneUser.setUserName(rs.getString("Name"));
				oneUser.setRole(rs.getString("role"));
				oneUser.setStatus(rs.getString("status"));
				oneUser.setType(rs.getString("type"));
				oneUser.setApplyDate(rs.getDate("applyDate"));
				oneUser.setFrom(rs.getDate("from"));
				oneUser.setTo(rs.getDate("to"));
				oneUser.setReason(rs.getString("reason"));
				oneUser.setLeavenature(rs.getString("leaveNature"));
				oneUser.setWorkDissemination(rs.getString("workDissemination"));
				oneUser.setContact(rs.getString("contactDetails"));
				
				
			}
			
		} catch (Exception e) {
			String error = "Error selecting users. Nested Exception is: " + e;
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
			throw new DAOSystemException(error);
		} finally {
			try {
				mysqlImpl.close();
			} catch (Exception e) {
			}
		}
		
		return oneUser;
				
	
	}
}

